package routers

import (
	v1 "gin-example/routers/api/v1"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"

	"gin-example/pkg/setting"
)

func InitRouter() *gin.Engine {
	r := gin.New()

	r.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"*"},
		AllowMethods:     []string{"PUT", "PATCH", "GET", "POST", "HEAD", "OPTIONS", "DELETE"},
		AllowHeaders:     []string{"Origin", "Authorization", "Content-Type", "x-token"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return true
		},
		MaxAge: 12 * time.Hour,
	}))

	r.Use(gin.Logger())

	r.Use(gin.Recovery())

	r.StaticFS("/static", http.Dir("./static"))

	gin.SetMode(setting.RunMode)

	apiv1 := r.Group("/api/v1")
	{
		apiv1.GET("/sendVerifyCode", v1.SendVerifyCode)
		apiv1.POST("/loginByPW", v1.LoginByPW)
		apiv1.POST("/loginByVC", v1.LoginByVC)
		apiv1.GET("/getAllUsers", v1.GetAllUsers)
		apiv1.POST("/register", v1.Register)
		apiv1.PUT("/updateUser/:id", v1.UpdateUser)
		apiv1.DELETE("/deleteUser/:id", v1.DeleteUser)

		apiv1.POST("/addImage", v1.AddImage)

		apiv1.GET("/getEmploymentsForUser", v1.GetEmploymentsForUser)
		apiv1.GET("/getEmploymentsForStation", v1.GetEmploymentsForStation)
		apiv1.POST("/addEmployment", v1.AddEmployment)
		apiv1.PUT("/updateEmployment/:id", v1.UpdateEmployment)
		apiv1.DELETE("/deleteEmployment/:id", v1.DeleteEmployment)

		apiv1.GET("/getStationsForUser", v1.GetStationsForUser)
		apiv1.GET("/getStationSlaves", v1.GetStationSlaves)
		apiv1.POST("/addStation", v1.AddStation)
		apiv1.PUT("/updateStation/:id", v1.UpdateStation)
		apiv1.DELETE("/deleteStation/:id", v1.DeleteStation)

		apiv1.GET("/getBSUsForUser", v1.GetBSUsForUser)
		apiv1.GET("/getBSUsByStationId", v1.GetBSUsByStationId)
		apiv1.GET("/getBSUSlaves", v1.GetBSUSlaves)
		apiv1.POST("/addBSU", v1.AddBSU)
		apiv1.PUT("/updateBSU/:id", v1.UpdateBSU)
		apiv1.DELETE("/deleteBSU/:id", v1.DeleteBSU)

		apiv1.GET("/getBCUsForUser", v1.GetBCUsForUser)
		apiv1.GET("/getBCUsByStationId", v1.GetBCUsByStationId)
		apiv1.GET("/getBCUsByBSUId", v1.GetBCUsByBSUId)
		apiv1.GET("/getBCUSlaves", v1.GetBCUSlaves)
		apiv1.POST("/addBCU", v1.AddBCU)
		apiv1.PUT("/updateBCU/:id", v1.UpdateBCU)
		apiv1.DELETE("/deleteBCU/:id", v1.DeleteBCU)

		apiv1.GET("/getBMUsForUser", v1.GetBMUsForUser)
		apiv1.GET("/getBMUsByStationId", v1.GetBMUsByStationId)
		apiv1.GET("/getBMUsByBSUId", v1.GetBMUsByBSUId)
		apiv1.GET("/getBMUsByBCUId", v1.GetBMUsByBCUId)
		apiv1.POST("/addBMU", v1.AddBMU)
		apiv1.PUT("/updateBMU/:id", v1.UpdateBMU)
		apiv1.DELETE("/deleteBMU/:id", v1.DeleteBMU)

		//apiv1.GET("/getCollectsByBMUId", v1.GetCollectsByBMUId)
		//apiv1.GET("/getCollectsByBCUId", v1.GetCollectsByBCUId)
		//apiv1.GET("/getCollectsByBSUId", v1.GetCollectsByBSUId)
		//apiv1.POST("/addCollect", v1.AddCollect)
		apiv1.GET("/getBMUCollects", v1.GetBMUCollects)
		apiv1.GET("/getBMUOutline", v1.GetBMUOutline)
		apiv1.GET("/getBCUCollects", v1.GetBCUCollects)
		apiv1.GET("/getBCUOutline", v1.GetBCUOutline)
		apiv1.POST("/addBMUCollect", v1.AddBMUCollect)
		apiv1.POST("/addBCUCollect", v1.AddBCUCollect)

		apiv1.GET("/getWarningsByBMUId", v1.GetWarningsByBMUId)
		apiv1.GET("/getWarningsByBCUId", v1.GetWarningsByBCUId)
		apiv1.GET("/getWarningsByBSUId", v1.GetWarningsByBSUId)
		apiv1.POST("/addWarningInBMU", v1.AddWarningInBMU)
		apiv1.POST("/addWarningInBCU", v1.AddWarningInBCU)
		apiv1.POST("/addWarningInBSU", v1.AddWarningInBSU)
		apiv1.PUT("/updateWarning/:id", v1.UpdateWarning)

		apiv1.GET("/getOrdersByBMUId", v1.GetOrdersByBMUId)
		apiv1.GET("/getOrdersByBCUId", v1.GetOrdersByBCUId)
		apiv1.GET("/getOrdersByBSUId", v1.GetOrdersByBSUId)
		apiv1.POST("/addOrderInBMU", v1.AddOrderInBMU)
		apiv1.POST("/addOrderInBCU", v1.AddOrderInBCU)
		apiv1.POST("/addOrderInBSU", v1.AddOrderInBSU)
		apiv1.PUT("/updateOrder/:id", v1.UpdateOrder)
		apiv1.DELETE("/deleteOrder/:id", v1.DeleteOrder)
	}
	return r
}